package com.blog.admin.service;

import com.blog.admin.mapper.BlogMessageMapper;
import com.blog.admin.vo.BlogMessageVO;
import com.blog.common.PageHelperLog;
import com.blog.vo.PageVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author tangqj
 * @Description 友情链接
 * @create 2020/2/18 14:35
 */
@Service
@Transactional
public class BlogMessageService {
    @Autowired
    BlogMessageMapper blogMessageMapper;

    /**
     * 分页获取留言
     * @param page
     * @return
     */
    @PageHelperLog(desc = "msg")
    public PageInfo<BlogMessageVO> getBlogMessage(PageVO page,BlogMessageVO blogMessageVO) {
        List<BlogMessageVO> list = blogMessageMapper.getBlogMessage(blogMessageVO);
        PageInfo<BlogMessageVO> pageRest = new PageInfo<BlogMessageVO>(list);
        return pageRest;
    }

    /**
     * 插入留言
     * @param blogMessageVO
     * @return
     */
    public boolean insertBlogMessage(BlogMessageVO blogMessageVO) {
        int result = blogMessageMapper.insertBlogMessage(blogMessageVO);
        return result>0?true:false;
    }


    /**
     * 删除留言
     * @param id
     * @return
     */
    public boolean deleteBlogMessageById(String id) {
        try {
            int result =  blogMessageMapper.deleteBlogMessageById(id);
            if (result < 1) {
                return false;
            }
            result = blogMessageMapper.deleteBlogMessageByReplyMessage(id);
            return result > 0 ? true : false;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键 手动回滚
            return false;
        }
    }


}
